package com.tarena.lbs.coupon.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.coupon.dao.mapper.CouponCodeMapper;
import com.tarena.lbs.coupon.dao.repository.CouponCodeRepository;
import com.tarena.lbs.coupon.pojo.po.CouponCodePO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class CouponCodeRepositoryImpl implements CouponCodeRepository {

    @Autowired
    private CouponCodeMapper couponCodeMapper;

    @Override
    public void insertCouponCode(CouponCodePO couponCodePO) {
        couponCodeMapper.insert(couponCodePO);
    }

    @Override
    public void deleteByCouponId(Integer id) {
        // 构建查询条件
        QueryWrapper<CouponCodePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coupon_id", id);
        // 执行删除操作
        couponCodeMapper.delete(queryWrapper);
    }

    @Override
    public List<CouponCodePO> selectList(CouponCodePO couponCodePO) {
        // 构建查询条件
        QueryWrapper<CouponCodePO> queryWrapper = generateQueryWrapper(couponCodePO);
        return couponCodeMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteById(Integer id) {
        couponCodeMapper.deleteById(id);
    }

    @Override
    public CouponCodePO selectByCode(String couponCode) {
        // 构建查询条件
        QueryWrapper<CouponCodePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coupon_code", couponCode);
        // 执行查询操作
        return couponCodeMapper.selectOne(queryWrapper);
    }

    private QueryWrapper<CouponCodePO> generateQueryWrapper(CouponCodePO couponCodePO) {
        QueryWrapper<CouponCodePO> queryWrapper = new QueryWrapper<>();
        // 根据需要添加查询条件
        if (couponCodePO.getCouponId() != null) {
            queryWrapper.eq("coupon_id", couponCodePO.getCouponId());
        }
        if (couponCodePO.getCouponCode()!= null) {
            queryWrapper.eq("coupon_code", couponCodePO.getCouponCode());
        }
        if (couponCodePO.getStatus()!= null) {
            queryWrapper.eq("status", couponCodePO.getStatus());
        }
        if (couponCodePO.getBusinessId()!= null) {
            queryWrapper.eq("business_id", couponCodePO.getBusinessId());
        }
        return queryWrapper;
    }
}
